package com.example.materialviewpagerdemo;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.NotificationCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.materialviewpagerdemo.Presenter.CurrentDataController;
import com.example.materialviewpagerdemo.Presenter.CurrentDataControllern;
import com.example.materialviewpagerdemo.bean.Dezhou;
import com.example.materialviewpagerdemo.bean.NanxiaoData;
import com.example.materialviewpagerdemo.bean.StaticData;
import com.example.materialviewpagerdemo.util.SPHelper;
import com.example.materialviewpagerdemo.view.OnItemClickListener;
import com.example.materialviewpagerdemo.view.Time;
import com.github.florent37.materialviewpager.MaterialViewPager;
import com.github.florent37.materialviewpager.header.HeaderDesign;

import cn.jzvd.Jzvd;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends DrawerActivity {
    private MaterialViewPager mViewPager;
    static final int TAPS = 2;
    //private DrawerLayout mDrawer;
    SPHelper spHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spHelper = new SPHelper(this, "URL");
        /*SPHelper.VauleMap vauleMap = new SPHelper.VauleMap("URL", "1");
        spHelper.putVaule(vauleMap);*/
        mViewPager = findViewById(R.id.materialViewPager);
        //添加监听
        mViewPager.setMaterialViewPagerListener(new MaterialViewPager.Listener() {
            @Override
            public HeaderDesign getHeaderDesign(int page) {
                switch (page) {
                    case 0:
                        return HeaderDesign.fromColorAndDrawable(
                                getResources().getColor(R.color.colorone),
                                getResources().getDrawable(R.drawable.head5));
                    case 1:
                        return HeaderDesign.fromColorAndDrawable(
                                getResources().getColor(R.color.green),
                                getResources().getDrawable(R.drawable.head5));

                    default:
                }

                //execute others actions if needed (ex : modify your header logo)

                return null;
            }
        });
        //设置Toolbar
        Toolbar toolbar = mViewPager.getToolbar();

        if (toolbar != null) {
            setSupportActionBar(toolbar);

            ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setDisplayShowTitleEnabled(true);
            actionBar.setDisplayUseLogoEnabled(false);
            actionBar.setHomeButtonEnabled(true);
        }

        //设置Adapter
        mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {

            @Override
            public Fragment getItem(int position) {

                switch (position % TAPS) {
                    case 0:
                        return TestFragment1.newInstance();
                        case 1:
                        return RecyclerViewFragmentn.newInstance();

                }
                return null;
            }

            @Override
            public int getCount() {
                return TAPS;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                switch (position % TAPS) {
                    case 0:
                        return "控制";
                    case 1:
                        return "数据";

                }
                return null;
            }
        });
        //设置setViewPager
        mViewPager.getViewPager().setOffscreenPageLimit(mViewPager.getViewPager().getAdapter().getCount());
        mViewPager.getPagerTitleStrip().setViewPager(mViewPager.getViewPager());
        for(int i=3;i<=4;i=i+1){
        SPHelper spHelper = new SPHelper(MainActivity.this, "URL");
        Retrofit retrofit = new Retrofit.Builder().baseUrl(StaticData.url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CurrentDataControllern currentDataController = retrofit.create(CurrentDataControllern.class);
        Call<NanxiaoData> currentDataCall = currentDataController.queryOne(i);
            int finalI = i;
            currentDataCall.enqueue(new Callback<NanxiaoData>() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onResponse(Call<NanxiaoData> call, Response<NanxiaoData> response) {
                System.out.println("======"+response.body());
                //  System.out.println("onresucss"+response.body().getDatetime());

                try {
                        double tem1=20.0,tem2=25.0;
                        double sun1=30.0,sun2=40.0;
                        double shidu1=45.0,shidu2=50.0;
                        double st1=20.0,st2=23.0;
                        double s1=65.0,s2=85.0;
                        String name="番茄";
                        if(finalI ==4){
                            tem1=25.0;
                            tem2=32.0;
                            shidu1=70.0;
                            shidu2=80.0;
                            sun1=40.0;
                            sun2=50.0;
                            s1=85.0;
                            s2=90.0;
                            name="黄瓜";
                        }




                } catch (NullPointerException e) {

                    Toast.makeText(MainActivity.this, "数据获取失败,请检查网络后重试", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<NanxiaoData> call, Throwable t) {
                t.printStackTrace();
               System.out.println("连接超时");
            }
        });}
    }


    @Override
    public void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }


    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public  void notify1(int i, String a){
        NotificationManager manager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        String id = "channel_11";
        String name = getString(R.string.app_name);

        // 创建NotificationChannel对象，传入id name 和 重要级别
        NotificationChannel notificationChannel =
                new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);

        // 创建通知的通道
        manager.createNotificationChannel(notificationChannel);

        // 创建通知
        // Builder中传入上下文对象和通道id
        Notification notification = new NotificationCompat
                .Builder(getBaseContext(), id)
                .setContentText("数据异常")
                .setContentTitle(a)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .build();
        // 发送通知
        manager.notify(i, notification);
    }
}
